const fs = require('fs');
const path = require('path');

/**
 * Recursively traverse a directory and find .js, .js.map, and .d.ts files.
 * @param {string} dir - Directory to start searching from
 */
function findCompiledFiles(dir) {
  // Read the contents of the directory
  fs.readdir(dir, { withFileTypes: true }, (err, files) => {
    if (err) {
      console.error(`Error reading directory ${dir}:`, err);
      return;
    }

    // Process each file or directory
    files.forEach((file) => {
      const fullPath = path.join(dir, file.name);

      if (file.isDirectory()) {
        // If it's a directory, recursively search it
        // if (file.name !== 'common') {
          findCompiledFiles(fullPath);
        // }
      } else {
        // If it's a file, check if it matches .js, .js.map, or .d.ts
        if (file.name.endsWith('.js') || file.name.endsWith('.js.map') || file.name.endsWith('.d.ts')) {
          if (file.name === 'jquery-1.9.1.js' || file.name === 'delete-compiled.js') {
            return;
          }
          console.log(fullPath); // Output the full path of the file
          fs.rmSync(fullPath);
        }
      }
    });
  });
}

// Start from the root of your project or specify a directory
const projectRoot = path.resolve('.\\');

// Call the function with the project root directory
findCompiledFiles(projectRoot);
